<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="昵称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入昵称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="账户" prop="account">
        <el-input
          v-model="queryParams.account"
          placeholder="请输入账户"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['consumer:user:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['consumer:user:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['consumer:user:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="唯一标识" align="center" prop="id" v-if="true"/>
      <el-table-column label="昵称" align="center" prop="nickName" />
      <el-table-column label="账户" align="center" prop="account" />
      <el-table-column label="设备号" align="center" prop="deviceId" />
      <el-table-column label="钻石" align="center" prop="diamond" />
      <el-table-column prop="isMember" align="center" label="是否会员" width="190">
        <template slot-scope="scope">
          <div v-if="scope.row.isMember=='0'">
            是否会员：是
            <br>
            会员级别：{{scope.row.memberLevel}}
            <br>
            到期时间：{{scope.row.memberExpireTime}}
          </div>
          <div v-if="scope.row.isMember=='1'">
            是否会员：否
            <br>
            会员级别：{{scope.row.memberLevel}}
            <br>
            到期时间：{{scope.row.memberExpireTime}}
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="isGridMember" align="center" label="是否网格会员" width="190">
        <template slot-scope="scope">
          <div v-if="scope.row.isGridMember=='0'">
            是否会员：是
            <br>
            会员级别：{{scope.row.gridMemberLevel}}
            <br>
            到期时间：{{scope.row.gridMemberExpireTime}}
          </div>
          <div v-if="scope.row.isGridMember=='1'">
            是否会员：否
            <br>
            会员级别：{{scope.row.gridMemberLevel}}
            <br>
            到期时间：{{scope.row.gridMemberExpireTime}}
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="spreadName" align="center" label="推广人">
        <template slot-scope="scope">
          <div v-if="scope.row.superiorType=='0'">
            推广人：{{scope.row.spreadName}}
            <br>
            上级类型：用户
          </div>
          <div v-if="scope.row.superiorType=='1'">
            推广人：{{scope.row.spreadName}}
            <br>
            上级类型：平台
          </div>
          <div v-if="scope.row.superiorType=='2'">
            推广人：{{scope.row.spreadName}}
            <br>
            上级类型：代理
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="shortVideoCount" align="center" label="赠送次数">
        <template slot-scope="scope">
          <div>
            短视频赠送次数：{{scope.row.shortVideoCount}}
            <br>
            长视频赠送次数：{{scope.row.longVideoCount}}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="最后登录IP" align="center" prop="loginIp" />
      <el-table-column label="最后登录时间" align="center" prop="loginDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.loginDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['consumer:user:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['consumer:user:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="昵称" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入昵称" />
        </el-form-item>
        <el-form-item label="账户" prop="account">
          <el-input v-model="form.account" placeholder="请输入账户" />
        </el-form-item>
        <el-form-item label="设备号" prop="deviceId">
          <el-input v-model="form.deviceId" placeholder="请输入设备号" />
        </el-form-item>
        <el-form-item label="钻石" prop="diamond">
          <el-input v-model="form.diamond" placeholder="请输入钻石" />
        </el-form-item>
        <el-form-item label="是否会员" prop="isMember">
          <el-radio-group v-model="form.isMember">
            <el-radio label="0">是</el-radio>
            <el-radio label="1">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="会员级别" prop="memberLevel" v-if="form.isMember == '0'">
          <el-radio-group v-model="form.memberLevel">
            <el-radio label="天会员">天会员</el-radio>
            <el-radio label="月会员">月会员</el-radio>
            <el-radio label="年度会员">年度会员</el-radio>
            <el-radio label="永久会员">永久会员</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="会员到期时间" prop="memberExpireTime"  v-if="form.isMember == '0'">
          <el-date-picker v-model="form.memberExpireTime" format="yyyy-MM-dd" value-format="yyyy-MM-dd"
                          :style="{width: '100%'}" placeholder="请选择日期选择" clearable/>
        </el-form-item>
        <el-form-item label="是否网格会员" prop="isGridMember">
          <el-radio-group v-model="form.isGridMember">
            <el-radio label="0">是</el-radio>
            <el-radio label="1">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="网格会员级别" prop="memberLevel" v-if="form.isGridMember == '0'">
          <el-radio-group v-model="form.memberLevel">
            <el-radio label="天会员">天会员</el-radio>
            <el-radio label="月会员">月会员</el-radio>
            <el-radio label="年度会员">年度会员</el-radio>
            <el-radio label="永久会员">永久会员</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="网格会员到期时间" prop="gridMemberExpireTime"  v-if="form.isGridMember == '0'">
          <el-date-picker v-model="form.gridMemberExpireTime" format="yyyy-MM-dd" value-format="yyyy-MM-dd"
                          :style="{width: '100%'}" placeholder="请选择日期选择" clearable></el-date-picker>
        </el-form-item>
        <el-form-item label="上级类型" prop="superiorType">
          <el-radio-group v-model="form.superiorType">
            <el-radio label="0">用户</el-radio>
            <el-radio label="1">平台</el-radio>
            <el-radio label="2">代理</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="推广人" prop="spreadId" v-if="form.superiorType == '0'">
          <el-select v-model="form.spreadId" placeholder="请选择用户">
            <el-option
              v-for="item in userInfoList"
              :key="item.id"
              :label="item.nickName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="推广人" prop="spreadId" v-if="form.superiorType == '1'">
          <el-input  placeholder="请输入推广人ID" value="admin" readonly='readonly'/>
        </el-form-item>
        <el-form-item label="推广人" prop="spreadId" v-if="form.superiorType == '2'">
          <el-select v-model="form.spreadId" placeholder="请选择代理">
            <el-option
              v-for="item in sysUserList"
              :key="item.userId"
              :label="item.nickName"
              :value="item.userId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="短视频赠送次数" prop="shortVideoCount">
          <el-input v-model="form.shortVideoCount" placeholder="请输入短视频赠送次数" />
        </el-form-item>
        <el-form-item label="长视频赠送次数" prop="longVideoCount">
          <el-input v-model="form.longVideoCount" placeholder="请输入长视频赠送次数" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listUser, getUser, delUser, addUser, updateUser, queryAllList} from "@/api/consumer/user";
import {selectUserList} from "@/api/system/user";

export default {
  name: "User",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户信息表格数据
      userList: [],
      userInfoList: [],
      sysUserList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nickName: undefined,
        account: undefined,
        deviceId: undefined,
        password: undefined,
        avatar: undefined,
        diamond: undefined,
        isMember: undefined,
        isGridMember: undefined,
        memberLevel: undefined,
        memberExpireTime: undefined,
        gridMemberLevel: undefined,
        gridMemberExpireTime: undefined,
        spreadId: undefined,
        superiorType: undefined,
        shortVideoCount: undefined,
        longVideoCount: undefined,
        status: undefined,
        loginIp: undefined,
        loginDate: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          { required: true, message: "唯一标识不能为空", trigger: "blur" }
        ],
        nickName: [
          { required: true, message: "昵称不能为空", trigger: "blur" }
        ],
        account: [
          { required: true, message: "账户不能为空", trigger: "blur" }
        ],
        deviceId: [
          { required: true, message: "设备号不能为空", trigger: "blur" }
        ],
        password: [
          { required: true, message: "登陆密码不能为空", trigger: "blur" }
        ],
        avatar: [
          { required: true, message: "头像地址不能为空", trigger: "blur" }
        ],
        diamond: [
          { required: true, message: "钻石不能为空", trigger: "blur" }
        ],
        isMember: [
          { required: true, message: "是否会员不能为空", trigger: "blur" }
        ],
        isGridMember: [
          { required: true, message: "是否网格会员不能为空", trigger: "blur" }
        ],
        memberLevel: [
          { required: true, message: "会员级别不能为空", trigger: "blur" }
        ],
        memberExpireTime: [
          { required: true, message: "会员到期时间不能为空", trigger: "blur" }
        ],
        gridMemberLevel: [
          { required: true, message: "网格会员级别不能为空", trigger: "blur" }
        ],
        gridMemberExpireTime: [
          { required: true, message: "网格会员到期时间不能为空", trigger: "blur" }
        ],
        spreadId: [
          { required: true, message: "推广人ID不能为空", trigger: "blur" }
        ],
        oneSuperiorId: [
          { required: true, message: "一级代理ID不能为空", trigger: "blur" }
        ],
        twoSuperiorId: [
          { required: true, message: "二级代理ID不能为空", trigger: "blur" }
        ],
        threeSuperiorId: [
          { required: true, message: "三级代理ID不能为空", trigger: "blur" }
        ],
        shortVideoCount: [
          { required: true, message: "短视频赠送次数不能为空", trigger: "blur" }
        ],
        longVideoCount: [
          { required: true, message: "长视频赠送次数不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "帐号状态不能为空", trigger: "change" }
        ],
        loginIp: [
          { required: true, message: "最后登录IP不能为空", trigger: "blur" }
        ],
        loginDate: [
          { required: true, message: "最后登录时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getSysUserList();
    this.getUserInfoList();
  },
  methods: {

    /** 查询用户信息列表 */
    getList() {
      this.loading = true;
      listUser(this.queryParams).then(response => {
        this.userList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getSysUserList() {
      selectUserList().then(response => {
        this.sysUserList = response.data;
      });
    },
    getUserInfoList() {
      queryAllList().then(response => {
        this.userInfoList = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        nickName: undefined,
        account: undefined,
        deviceId: undefined,
        password: undefined,
        avatar: undefined,
        diamond: undefined,
        isMember: undefined,
        isGridMember: undefined,
        memberLevel: undefined,
        memberExpireTime: undefined,
        gridMemberLevel: undefined,
        gridMemberExpireTime: undefined,
        spreadId: undefined,
        oneSuperiorId: undefined,
        twoSuperiorId: undefined,
        threeSuperiorId: undefined,
        shortVideoCount: undefined,
        longVideoCount: undefined,
        status: undefined,
        delFlag: undefined,
        loginIp: undefined,
        loginDate: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getUser(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改用户信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateUser(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addUser(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除用户信息编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delUser(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('consumer/user/export', {
        ...this.queryParams
      }, `user_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
